package com.lv.service.dialog

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.bean.CommonSelectedBean
import com.lv.common.imp.OnPositionTListener
import com.lv.service.R
import com.lv.service.adapter.RecyclerSelectedTag2Adapter
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class SelectedShopTopGridPopupView(
    private val mContext: Context,
    private val mAllBeans: MutableList<CommonSelectedBean>,
    private val positionListener: OnPositionTListener<MutableList<CommonSelectedBean>>? = null,

    ) :
    PartShadowPopupView(mContext) {
    private var mAdapter: RecyclerSelectedTag2Adapter? = null
    private var mRvContent: RecyclerView? = null
    private var viewBottom: View? = null

    override fun getImplLayoutId(): Int {
        return R.layout.selected_single_top_grid_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewBottom = findViewById<View>(R.id.view_bottom)
        mRvContent = findViewById<RecyclerView>(R.id.rv_content)
        initView()
        initEvnet()
    }

    private fun initView() {
        val gridLayoutManager = GridLayoutManager(mContext, 2)
        mRvContent?.layoutManager = gridLayoutManager
        mAdapter = RecyclerSelectedTag2Adapter(mAllBeans)
        mRvContent?.adapter = mAdapter
        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            val selected = data?.selected ?: false
            val tag = data?.tag
            if (selected) {
                mAllBeans[position].selected = false
                mAdapter?.notifyItemChanged(position, R.id.tv_name)
            } else {
                when (tag) {
                    "0" -> {
                        mAllBeans.forEach { it.selected = false }
                        mAllBeans[position].selected = true
                        mAdapter?.notifyDataSetChanged()
                    }

                    else -> {
                        mAllBeans[position].selected = true
                        mAdapter?.notifyItemChanged(position, R.id.tv_name)
                        val index = mAllBeans.indexOfFirst { it.tag == "0" }
                        if (index >= 0) {
                            mAllBeans[index].selected = false
                            mAdapter?.notifyItemChanged(index, R.id.tv_name)
                        }
                    }
                }
            }

        }

    }

    private fun initEvnet() {

        viewBottom?.setOnClickListener {
            positionListener?.onClick(0, mAllBeans)
            dismiss()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()
        positionListener?.onClick(0, mAllBeans)

    }


}